package com.itcncp.ucenter.web.controller;

import com.itcncp.common.base.BaseController;
import com.itcncp.ucenter.common.ApiResult;
import com.itcncp.ucenter.common.ApiResultConstant;
import com.itcncp.ucenter.dao.model.*;
import com.itcncp.ucenter.rpc.api.ItcsExitWhitelistRgtService;
import com.itcncp.ucenter.rpc.api.ItcsTransJnlService;
import com.itcncp.ucenter.rpc.api.UserCarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("/transJnl")
@Api(tags = "transJnl",value = "通行记录", description = "通行记录",
        produces = "transJnl",consumes = "transJnl",basePath = "/transJnl")
class TransJnlController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger( TransJnlController.class);

    @Autowired
    private ItcsTransJnlService itcsTransJnlService;

    @Autowired
    private ItcsExitWhitelistRgtService itcsExitWhitelistRgtService;

    @Autowired
    private UserCarInfoService  userCarInfoService;

    /**
     *  判断用户白名单是否已下发
     */
    @ResponseBody
    @ApiOperation(value = "判断用户白名单是否已下发")
    @ApiResponse(code=200,message = "判断用户白名单是否已下发",response = ItcsExitWhitelistRgt.class)
    @RequestMapping(value = "/isExitWhitelist", method = RequestMethod.POST)
    public Object isExitWhitelist(HttpServletRequest request,
                                  @RequestParam(required = false, defaultValue = "0", value = "userId") Long userId,
                                      @RequestParam(required = false, defaultValue = "0", value = "tollstationId") String tollstationId){

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        ItcsExitWhitelistRgtExample itcsExitWhitelistRgtExample = new ItcsExitWhitelistRgtExample();
        itcsExitWhitelistRgtExample.createCriteria()
                .andUserIdEqualTo(userId)
                .andStationIdEqualTo(tollstationId);

        Integer total =  itcsExitWhitelistRgtService.countByExample(itcsExitWhitelistRgtExample);

        //获取用户车牌数量
        UserCarInfoExample userCarInfoExample = new UserCarInfoExample();
        //数据是否删除  1、删除  2、不删除
        userCarInfoExample.createCriteria()
        .andDelEqualTo( new Byte( "2" ) )
        .andUserIdEqualTo( userId );
        Integer totalcar = userCarInfoService.countByExample(userCarInfoExample);


        //判断白名单车牌与所绑定的车牌数据量是否一致,如果一致则白名单存在
        if(total == totalcar){
            return  new ApiResult( ApiResultConstant.APP_EXIT_WHITELIST_EXIST);
        }else{
//            //清空白名单 并重发
//            itcsExitWhitelistRgtService.deleteByExample(itcsExitWhitelistRgtExample);

            return new ApiResult( ApiResultConstant.SUCCESS);
        }
    }

    /**
     *  判断用户白名单是否已下发
     */
    @ResponseBody
    @ApiOperation(value = "清空白名单")
    @ApiResponse(code=200,message = "清空白名单",response = ItcsExitWhitelistRgt.class)
    @RequestMapping(value = "/clearExitWhitelist", method = RequestMethod.POST)
    public Object ClearExitWhitelist(HttpServletRequest request,
                                  @RequestParam(required = false, defaultValue = "0", value = "userId") Long userId){

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        ItcsExitWhitelistRgtExample itcsExitWhitelistRgtExample = new ItcsExitWhitelistRgtExample();
        itcsExitWhitelistRgtExample.createCriteria()
                .andUserIdEqualTo(userId);

        itcsExitWhitelistRgtService.deleteByExample(itcsExitWhitelistRgtExample);

        return  new ApiResult( ApiResultConstant.SUCCESS);
    }



    /**
     * 获取通行记录信息
     * 需要优化，不可以从总表查询
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "获取通行记录信息")
    @ApiResponse(code=200,message = "获取通行记录信息",response = ItcsTransJnl.class)
    @RequestMapping(value = "/findByDrivingRecord", method = RequestMethod.POST)
    public Object findByDrivingRecord(HttpServletRequest request,
                                        @RequestParam(required = false, defaultValue = "0", value = "userId") Long userId,
                                        @RequestParam(required = false, defaultValue = "0", value = "pageSize") int pageSize,
                                        @RequestParam(required = false, defaultValue = "5", value = "pageIndex") int pageIndex,
                                        @RequestParam(required = false, defaultValue = "", value = "search") String search,
                                        @RequestParam(required = false, defaultValue = "", value = "plateno") String plateno){

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        ItcsTransJnlExample itcsTransJnlExample = new ItcsTransJnlExample();

        ItcsTransJnlExample.Criteria criteria = itcsTransJnlExample.createCriteria();
        criteria.andUserIdEqualTo( userId );

        //模糊查询
        if(StringUtils.isNotEmpty( search )) {
            search = "%"+search+"%";
            itcsTransJnlExample.or().andCarPlateLike( search );
        }
        //根据车牌号查询
        if(StringUtils.isNotEmpty( plateno )) {
            search = "%"+plateno+"%";
            criteria.andCarPlateEqualTo(plateno);
        }

        //排序
        itcsTransJnlExample.setOrderByClause("toll_time desc");

        //分页查询
        List<ItcsTransJnl> itcsTransJnls = itcsTransJnlService.selectByExampleForStartPage(itcsTransJnlExample,pageIndex,pageSize);

        return new ApiResult( ApiResultConstant.SUCCESS,itcsTransJnls);
    }


    /**
     * 获取通行记录信息根据车牌汇总
     * 需要优化，不可以从总表查询
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "获取通行记录信息根据车牌汇总")
    @ApiResponse(code=200,message = "获取通行记录信息根据车牌汇总",response = ItcsTransJnl.class)
    @RequestMapping(value = "/totalPlateTollFare", method = RequestMethod.POST)
    public Object findByDrivingRecord(HttpServletRequest request,
                                      @RequestParam(required = false, defaultValue = "0", value = "userId") Long userId,
                                      @RequestParam(required = false, defaultValue = "", value = "plateno") String plateno){

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        //根据车牌号查询
        if(StringUtils.isEmpty( plateno )) {
            return new ApiResult(ApiResultConstant.FAILED);
        }

        ItcsTransJnl itcsTransJnl = itcsTransJnlService.totalPlateTollFare(userId,plateno);

        return new ApiResult( ApiResultConstant.SUCCESS,itcsTransJnl);
    }


    /**
     * 获取通行记录信息
     * 需要优化，不可以从总表查询
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "获取未开票的信息")
    @ApiResponse(code=200,message = "获取未开票的信息",response = ItcsTransJnl.class)
    @RequestMapping(value = "/findByInvoice", method = RequestMethod.POST)
    public Object findByInvoice(HttpServletRequest request,
                                      @RequestParam(required = false, defaultValue = "0", value = "userId") Long userId,
                                      @RequestParam(required = false, defaultValue = "0", value = "pageSize") int pageSize,
                                      @RequestParam(required = false, defaultValue = "", value = "pageIndex") int pageIndex,
                                      @RequestParam(required = false, defaultValue = "1", value = "invoiceState") Integer invoiceState,
                                      @RequestParam(required = false, defaultValue = "", value = "search") String search){

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        ItcsTransJnlExample itcsTransJnlExample = new ItcsTransJnlExample();

        ItcsTransJnlExample.Criteria criteria = itcsTransJnlExample.createCriteria();
        criteria.andUserIdEqualTo( userId );
        //是否已开发票 1、未开 2、已开票
        if(invoiceState==1){
            //查询未开票信息
            criteria.andInvoiceStateNotEqualTo(new Byte("2"));
        }else{
            //查询已开票信息
            criteria.andInvoiceStateEqualTo(new Byte("2"));
        }

        //模糊查询
        if(StringUtils.isNotEmpty( search )) {
            search = "%"+search+"%";
            itcsTransJnlExample.or().andCarPlateLike( search );
        }

        //排序
        itcsTransJnlExample.setOrderByClause("toll_time desc");

        //分页查询
        List<ItcsTransJnl> itcsTransJnls = itcsTransJnlService.selectByExampleForStartPage(itcsTransJnlExample,pageIndex,pageSize);

        return new ApiResult( ApiResultConstant.SUCCESS,itcsTransJnls);
    }

}